﻿using System;
using System.Linq;
using System.Xml.Linq;
using System.Globalization;

using Matrix.Xmpp.Vcard;

using SBPweb.Communication.Shared.Library.Contacts;
using SBPweb.Communication.Shared.Library.Profiles;
using Matrix;

namespace SBPweb.Communication.Client.Library.Xmpp.Profiles
{

    partial class Profile : ProfileBase
    {

        #region Protected fields

        protected Vcard vCard;

        #endregion

        #region Constructor logic

        internal Profile(IContact contact, Vcard vCard)
            : base(contact)
        {
            this.vCard = vCard;
        }

        #endregion

        #region Property accessors

        public override string FullName
        {
            get
            {
                return vCard.Fullname;
            }
            set
            {
                vCard.Fullname = value;
            }
        }

        public override string NickName
        {
            get
            {
                return vCard.Nickname;
            }
            set
            {
                vCard.Nickname = value;
            }
        }

        public override DateTime BirthDay
        {
            get
            {
                return vCard.Birthday;
            }
            set
            {
                vCard.Birthday = value;
            }
        }

        public override string Email
        {
            get
            {
                return Get<string>("Email");
            }
            set
            {
                Put("Email", value);
            }
        }

        #endregion

        #region Methods

        public override T Get<T>(string propertyName)
        {
            string ret = vCard.GetAttribute(propertyName);
            if (string.IsNullOrEmpty(ret))
                return default(T);
            else
                return (T)Convert.ChangeType(ret, typeof(T), CultureInfo.InvariantCulture);
        }

        public override void Put<T>(string propertyName, T value)
        {
            vCard.SetAttribute(propertyName, value.ToString());
        }

        #endregion

    }

}